package com.chen.mobiletwo.service;

import com.chen.mobiletwo.dao.UserDao;
import com.chen.mobiletwo.entity.ResultEntity;
import com.chen.mobiletwo.tool.MD5Tool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserService {//用户服务类
    private static final Logger logger= LoggerFactory.getLogger(UserService.class);//日志getLogger需要传入的类名
    @Resource
    UserDao userDao;
    @Resource
    MD5Tool md5Tool;
   /**
     * 重置密码为123456
     * @param uId 用户id
     * @return String json
     */
    public String reSetPwd(int uId){
        try{
            return ResultEntity.success(userDao.rePwd(uId,"202cb962ac59075b964b07152d234b70"));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }

    }

    /**
     * 重置密码
     * @param uId 用户id
     * @param pwd 密码
     * @return String（Json）
     */
    public String setPwd(int uId,String pwd){
        try{
            return ResultEntity.success(userDao.rePwd(uId, md5Tool.ToMD5(pwd)));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     *  根据id软删除数据
     * @param uId 用户id
     * @return json
     */
    public String delUser(int uId){
        try{
            return ResultEntity.success(userDao.delUser(uId));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     * 获取与用户列表（有分页）
     * @param uName 用户名
     * @param start  起始位置
     * @param cnt 数量
     * @return string
     */
    public String getUserList (String uName,int start,int cnt){
        try{
            return ResultEntity.success(userDao.getUserList(uName,start,cnt));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     * 根据指定用户查询用户详情信息
     * @param phone 手机号
     * @param weChat 微信号
     * @param email 邮箱号
     * @return String
     */
    public String getDetailList (String phone,String weChat,String email){
        try{
            return ResultEntity.success(userDao.getDetailList(phone,weChat,email));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     * 增加用户 默认密码123456
     * @param uName 用户名
     * @return String
     */
    public String addUser (String uName){
        try{
            return ResultEntity.success(userDao.addUser(uName));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     * 新增用户详情
     * @param uId 用户id
     * @param phone 电话
     * @param weChat 微信
     * @param qqNum qq
     * @param email 邮箱
     * @return String
     */
    public String addUserDetail (int uId,String phone,String weChat,String qqNum,String email){
        try{
            String detailId= UUID.randomUUID().toString();//生成全局唯一ID
            return ResultEntity.success(userDao.addUserDetail(detailId,uId,phone,weChat,qqNum,email));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }
}
